'''
Author: your name
Date: 2021-11-06 20:07:02
LastEditTime: 2021-11-12 11:48:07
LastEditors: Please set LastEditors
Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
FilePath: /tinyms/tinyms-api/apps/cms/channel/views.py
'''


# from crypt import methods
from flask import request
from . import cms_channel
from .model import Channel
from .service import ChannelService
from apps.utils import auty
from apps.utils.TypeUtils import TypeUtils
import json
import time

'''
分页查询
'''
@cms_channel.route('/pages',methods = ['GET','POST'])
@auty.login_required
def pages():
    if request.method == "POST":
        page = request.form.get("page")
    else:
        page = request.args.get("page")
    pages =  ChannelService.pages(page)
    data = {
        'msg':'success',
        'code':0,
        'data':[TypeUtils.list_to_dict(channel) for channel in pages.items]
    }
    return json.dumps(data,ensure_ascii=False)

'''
通过主键删除
'''
@cms_channel.route('/delete',methods=['GET','POST'])
@auty.login_required
def delete():
    if request.method == 'POST':
        channel_pk = request.form.get("channel_pk")
    else:
        channel_pk = request.args.get("channel_pk")
    ChannelService.delete(channel_pk)
    data = {
        'msg':'删除成功',
        'code':'0',
        'data':''
    }
    return json.dumps(data)

'''
查询菜单列表包含按钮
用于角色授权时显示所有菜单及按钮
'''
@cms_channel.route('/list',methods=['GET','POST'])
def tree_menu_list():
    if request.method == 'POST':
        channel_state = request.form.get("channel_state")
    else:
        channel_state = request.args.get("channel_state")   
    if not channel_state: 
        channel_state = None
    #必须实例化，否则无法调用递归
    channelservice = ChannelService()
    channels = channelservice.list(channel_state)
    return channels

@cms_channel.route("/saveUpdate",methods=["POST","GET"])
@auty.login_required
def saveUpdate():
    data = {}
    if request.method == "POST":
        for key in request.form:
            data[key] = request.form.get(key)
    else:
        for key in request.args:
            data[key] = request.args.get(key)
    channel = Channel()
    channel.channel_name = data['channel_name']
    channel.channel_parent = data['channel_parent']
    channel.channel_english = data['channel_english']
    channel.channel_path = data['channel_path']
    channel.channel_content_template = data['channel_content_template']
    channel.channel_page_template = data['channel_page_template']
    channel.channel_page_count = data['channel_page_count']
    channel.channel_list_template = data['channel_list_template']
    channel.channel_list_count = data['channel_list_count']
    channel.channel_state = data['channel_state']
    if not data['channel_pk']:
            # article.article_pk = str(uuid.uuid1())
        ChannelService.save(channel)
    else:
        channel.channel_pk=data['channel_pk']
        ChannelService.update(channel)
    data = {
        'code':'0',
        'msg':'添加成功'
    }
    return json.dumps(data)